package com.novellotus.iov.carvice.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.anim.Anim;
import com.novellotus.iov.carvice.anim.AnimCaChu;
import com.novellotus.iov.carvice.anim.EnterAnimLayout;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;
import com.novellotus.iov.carvice.utils.MyUtils;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class CurrentChargeGraphFragment extends BaseFragment {


    @BindView(R.id.anim_layout)
    EnterAnimLayout anim_layout;
    Anim anim;
    //电机
    @BindView(R.id.rlt_charge)
    RelativeLayout rlt_charge;
    ImageView img_charge;
    TextView tv_charge_word;
    ImageView img_triangle;
    //电机
    @BindView(R.id.rlt_motor)
    RelativeLayout rlt_motor;
    ImageView img_motor;
    TextView tv_motor_word;
    //控制器
    @BindView(R.id.rlt_control)
    RelativeLayout rlt_control;
    ImageView img_control;
    TextView tv_control_word;
    //高压盒
    @BindView(R.id.rlt_height_box)
    RelativeLayout rlt_height_box;
    ImageView img_height_box;
    TextView tv_height_box_word;

    @BindView(R.id.tv_plate)
    TextView tv_plate;
    @BindView(R.id.tv_vin)
    TextView tv_vin;
    @BindView(R.id.tv_height_voltage_value)
    TextView tv_height_voltage_value;
    @BindView(R.id.tv_low_voltage_value)
    TextView tv_low_voltage_value;
    @BindView(R.id.tv_height_temperature_value)
    TextView tv_height_temperature_value;
    @BindView(R.id.tv_low_temperature_value)
    TextView tv_low_temperature_value;

    @BindView(R.id.rlt_current_box)
    RelativeLayout rlt_current_box;
    @BindView(R.id.tv_current_box_word)
    TextView tv_current_box_word;


    @BindView(R.id.tv_height_box_voltage)
    TextView tv_height_box_voltage;

    @BindView(R.id.tv_height_box_current)
    TextView tv_height_box_current;

    private RxDiagnosisAlarm rxDiagnosisAlarm;

    public CurrentChargeGraphFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            rxDiagnosisAlarm = getArguments().getParcelable("alarm");
        }
    }

    public static CurrentChargeGraphFragment newInstance(RxDiagnosisAlarm rxDiagnosisAlarm) {
        CurrentChargeGraphFragment fragment = new CurrentChargeGraphFragment();
        Bundle args = new Bundle();
        args.putParcelable("alarm", rxDiagnosisAlarm);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    View initRootView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_current_charge_graph, container, false);
    }

    private void initViews() {
        //充电接口
        img_charge = rlt_charge.findViewById(R.id.img_background);
        img_charge.setBackgroundResource(R.mipmap.icon_charge_white);
        tv_charge_word = rlt_charge.findViewById(R.id.tv_word);
        //电机
        img_motor = rlt_motor.findViewById(R.id.img_background);
        img_motor.setBackgroundResource(R.mipmap.icon_motor_white);
        tv_motor_word = rlt_motor.findViewById(R.id.tv_word);
        //控制器
        img_control = rlt_control.findViewById(R.id.img_background);
        img_control.setBackgroundResource(R.mipmap.icon_control_white);
        tv_control_word = rlt_control.findViewById(R.id.tv_word);
        //高压盒
        img_height_box = rlt_height_box.findViewById(R.id.img_background);
        img_height_box.setBackgroundResource(R.mipmap.icon_height_box_white);
        tv_height_box_word = rlt_height_box.findViewById(R.id.tv_word);
        //电池
        rlt_current_box.setBackgroundResource(R.mipmap.icon_current_white_box);
    }

    @Override
    void init() {

        initViews();
        if (rxDiagnosisAlarm != null) {
            tv_height_box_voltage.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getTotal_voltage() + "") + "V");
            tv_height_box_current.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getTotal_current() + "") + "A");
//            tv_control_voltage.setText(MyUtils.getRealValue(rxDiagnosisAlarm.getModelValues().getControl_in_voltage() + "") + "V");
//            tv_control_current.setText(MyUtils.getRealValue(rxDiagnosisAlarm.getModelValues().getControl_bus_current() + "") + "A");
            tv_plate.setText(rxDiagnosisAlarm.getPn());
            tv_vin.setText(rxDiagnosisAlarm.getVin());
            tv_height_voltage_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getBattery_single_highest_voltage() + "") + "V  " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_voltage_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_voltage_battery_single_num()));
            tv_low_voltage_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getBattery_single_lowest_voltage() + "") + "V  " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_voltage_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_voltage_battery_single_num()));
            tv_height_temperature_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_temp() + "") + "℃  " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_temp_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_temp_battery_probe_num()));
            tv_low_temperature_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_temp() + "") + "℃ " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_temp_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_temp_battery_probe_num()));
        }
        setAlarmComponents();
        img_triangle = rlt_charge.findViewById(R.id.img_triangle);
        img_triangle.setVisibility(View.GONE);
        anim_layout.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                anim = new AnimCaChu(anim_layout);
                handler.postDelayed(runnable, 0);
                anim_layout.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });


    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            anim.startAnimation(5000);
            handler.postDelayed(this, 3000);
        }
    };

    private void setAlarmComponents() {

        for (int i = 0; i < rxDiagnosisAlarm.getComponents().size(); i++) {
            switch (rxDiagnosisAlarm.getComponents().get(i).getCompCode()) {
                case "comp_charge_port":
                    img_charge.setBackgroundResource(R.mipmap.icon_charge_blue);
                    tv_charge_word.setVisibility(View.VISIBLE);
                    tv_charge_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    break;
                case "comp_motor":
                    //电机
                    img_motor.setBackgroundResource(R.mipmap.icon_motor_blue);
                    tv_motor_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_motor_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_controller":
                    //控制器
                    img_control.setBackgroundResource(R.mipmap.icon_control_blue);
                    tv_control_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_control_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_high_voltage":
                    //高压盒
                    img_height_box.setBackgroundResource(R.mipmap.icon_height_box_blue);
                    tv_height_box_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_height_box_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_battery":
                    //电池
                    rlt_current_box.setBackgroundResource(R.mipmap.icon_current_blue_box);
                    tv_current_box_word.setVisibility(View.VISIBLE);
                    tv_current_box_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    break;
            }
        }

    }

}
